<?php
/**
 * @title 421. 数组中两个数的最大异或值
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class TreeNode{
    public $val = null;
    public $left = null;
    public $right = null;

    function __construct($val = null, $left = null, $right = null)
    {
        $this->val = $val;
        $this->left = $left;
        $this->right = $right;
    }
}

class Solution {

    /**
     * @param Integer[] $nums
     * @return Integer
     */
    function findMaximumXOR($nums) {
        // $numsString = file_get_contents("/tmp/leetcode/2023-11-04.txt");
        // $nums = explode(",", $numsString);
        // return count($nums);

        /**
         * @since 2023-11-23 31维层次数组, Tree创建耗费时间
         */
        $levelArray = array_fill(0, 31, []);

        /**
         * @since 2023-11-23 数组长度，2*10^5
         */
        $len = count($nums);

        /**
         * @since 2023-11-23 放到树的子节点上
         */
        $binArray = [];
        for ($i=0; $i<$len; $i++){
            /**
             * @since 2023-11-23 转成二进制字符串
             */
            $num = $nums[$i];
            $numBin = self::GetBin($num);
            $binArray[] = $numBin;
            // echo "num={$num}, {$numBin} = (", strlen($numBin), ")", PHP_EOL;

            $k = 0;
            for($h=0; $h<31; $h++){
                /**
                 * [
                 *  [0,1],
                 *  [0,1,2,3],
                 *  ...
                 * ]
                 *
                 *      root
                 *     /    \
                 *    0      1
                 *   / \    / \
                 *  0   1  2   3
                 */
                $k = ($k<<1) + $numBin[$h];
                $levelArray[$h][$k] = $num;
            }
        }
        // return false;
        
        /**
         * @since 2023-11-23 每个数字对应异或最大值
         */
        $maxXor = 0;
        for($i=0; $i<$len; $i++){
            /**
             * @since 2023-11-23 转成二进制字符串
             */
            $num = $nums[$i];
            $numBin = $binArray[$i];
            // echo "num={$num}, {$numBin}", PHP_EOL, PHP_EOL;

            /**
             * @since 2023-12-01 查找异或最大值
             */
            $k = 0;
            for($h=0; $h<31; $h++){
                $leftKey = $k<<1;
                $rightKey = $leftKey+1;
                if($numBin[$h] == 0){
                    /**
                     * @since 2023-12-01 右边优先
                     */
                    if(isset($levelArray[$h][$rightKey])){
                        $k = $rightKey;
                    } else {
                        $k = $leftKey;
                    }
                } else {
                    /**
                     * @since 2023-12-01 左边优先
                     */
                    if(isset($levelArray[$h][$leftKey])){
                        $k = $leftKey;
                    } else {
                        $k = $rightKey;
                    }
                }
            }

            /**
             * @since 2023-11-23 异或
             */
            $numXor = $levelArray[30][$k];
            $maxXor = max($maxXor, $num ^ $numXor);
        }

        /**
         * @return
         */
        return $maxXor;
    }

    /**
     * @title 返回31位二进制字符串
     * @author start2004
     * @since 2023-11-23 Created
     *
     * @param Int $num
     * @return string
     */
    function GetBin($num = 0){
        /**
         * @since 2023-11-23 转换二进制，高位不够补0，180ms
         */
        // $numBin = strval(decbin($num));
        // $numBin = str_repeat("0", 31 - strlen($numBin)) . $numBin;

        /**
         * @since 2023-11-23 转换成31位二进制，300ms
         */
        // $numBin = sprintf("%031d", decbin($num));

        /**
         * @since 2023-11-23 转换二进制并补齐高位，180ms
         */
        $numBin = str_pad(decbin($num), 31, "0", STR_PAD_LEFT);

        /**
         * @since 2023-11-23 移位操作和与运算AND，700ms
         */
        // $numBin = "";
        // for($i=30; $i>=0; $i--){
        //     $numBin .= (($num>>$i) & 1);
        // }

        /**
         * @return
         */
        return $numBin;
    }
}


/**
 * @url http://127.0.0.1/leetcode/202311/2023.11.04_3.php
 */
$datas = [
    [[3,10,5,25,2,8]], // 28
    // [[14,70,53,83,49,91,36,80,92,51,66,70]], // 127
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    if(count($data) <= 20){
        var_dump($data);
    } else {
        var_dump(count($data));
    }

    $obj = new Solution();
    $result = $obj->findMaximumXOR(...$data);
    // $result = $obj->($xhprof->tree($data));
    // $result = $obj->($xhprof->listNode($data));
    // $result = $obj->find(...$data);
    var_dump($result);
    // if(count($result)<=20){
    //     var_dump($result);
    // } else {
    //     var_dump(count($result));
    // }
    echo str_repeat(PHP_EOL, 3);
    echo str_repeat("<br>", 3);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
